//
//  MYSelectCoverView.m
//  MiaoYin
//
//  Created by juweitu on 2020/7/8.
//  Copyright © 2020 thy. All rights reserved.
//

#import "MYSelectCoverView.h"

@interface MYSelectCoverView ()
{
    UIView *my_mainView;
}
@property (nonatomic,copy) MYSelectCoverCompletion completion;
@end

@implementation MYSelectCoverView

- (void)showWithCompletion:(MYSelectCoverCompletion)completion
{
    [[[[UIApplication sharedApplication] delegate] window] addSubview:self];
    
    self.completion = completion;
    
    [UIView animateWithDuration:0.3 animations:^{
        self->my_mainView.transform = CGAffineTransformTranslate(self->my_mainView.transform, 0, -self->my_mainView.bounds.size.height);
    } completion:^(BOOL finished) {
    }];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    
    if(self = [super initWithFrame:frame])
    {
        [self layout];
    }
    
    return self;
}

- (void)layout
{
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
    
    my_mainView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT*0.6, SCREEN_WIDTH, SCREEN_HEIGHT*0.4)];
    my_mainView.backgroundColor = [UIColor whiteColor];
    my_mainView.clipsToBounds = YES;
    my_mainView.alpha = 1.0;
    [self addSubview:my_mainView];
    
    UILabel *hintLabel = [[UILabel alloc] init];
    hintLabel.textColor = MY_Text_Color_Theme1;
    hintLabel.font = [UIFont boldSystemFontOfSize:14];
    hintLabel.text = @"更改封面";
    [my_mainView addSubview:hintLabel];
    
    UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeButton setImage:[UIImage imageNamed:@"ic_shop_close"] forState:0];
    [closeButton addTarget:self action:@selector(closeButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
    [my_mainView addSubview:closeButton];
    
    UIImageView *iconImgView = [[UIImageView alloc] init];
    iconImgView.image = [UIImage imageNamed:@"ic_tips_toast"];
    [my_mainView addSubview:iconImgView];
    
    UILabel *hint2Label = [[UILabel alloc] init];
    hint2Label.textColor = MY_Text_Color_Theme3;
    hint2Label.font = [UIFont systemFontOfSize:12];
    hint2Label.text = @"海报为人工审核，请勿上传暴露/低俗/包含边框、相机水印的图片，海报如在审核中/被驳回，会保持原海报不变。";
    hint2Label.numberOfLines = 0;
    [my_mainView addSubview:hint2Label];
    
    UIButton *videoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [videoButton setTitle:@"从该段视频中选择" forState:0];
    [videoButton addTarget:self action:@selector(videoButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
    [videoButton setTitleColor:[UIColor blackColor] forState:0];
    videoButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [my_mainView addSubview:videoButton];
    
    UIButton *photoAlbumButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [photoAlbumButton setTitle:@"从手机相册选择" forState:0];
    [photoAlbumButton addTarget:self action:@selector(photoAlbumButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
    [photoAlbumButton setTitleColor:[UIColor blackColor] forState:0];
    photoAlbumButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [my_mainView addSubview:photoAlbumButton];
    
    UIButton *cameraButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cameraButton setTitle:@"拍摄" forState:0];
    [cameraButton addTarget:self action:@selector(cameraButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
    [cameraButton setTitleColor:[UIColor blackColor] forState:0];
    cameraButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [my_mainView addSubview:cameraButton];
    
    {
        [hintLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(MY_Padding));
            make.top.equalTo(@30);
        }];
        
        [closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(hintLabel);
            make.height.width.equalTo(@36);
            make.right.equalTo(@(-MY_Padding));
        }];
        
        [iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(hintLabel);
            make.top.equalTo(hintLabel.mas_bottom).offset(27);
            make.width.height.equalTo(@16);
        }];
        
        [hint2Label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(hintLabel.mas_bottom).offset(25);
            make.left.equalTo(iconImgView.mas_right).offset(8);
            make.right.equalTo(@(-MY_Padding));
        }];
        
        [videoButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self->my_mainView);
            make.height.equalTo(@50);
            make.top.equalTo(hint2Label.mas_bottom).offset(30);
        }];
        
        [photoAlbumButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.height.equalTo(videoButton);
            make.top.equalTo(videoButton.mas_bottom);
        }];
        
        [cameraButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.height.equalTo(videoButton);
            make.top.equalTo(photoAlbumButton.mas_bottom);
        }];
    }
    
    UIBezierPath* maskPath = [UIBezierPath bezierPathWithRoundedRect:my_mainView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(12, 12)];
    CAShapeLayer* maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = my_mainView.bounds;
    maskLayer.path = maskPath.CGPath;
    my_mainView.layer.mask = maskLayer;
    
    my_mainView.transform = CGAffineTransformTranslate(my_mainView.transform, 0, my_mainView.bounds.size.height);
}

- (void)photoAlbumButtonTouchUpInside:(UIButton *)sender
{
    if (self.completion)
    {
        self.completion(MYSelectCoverTypePhotoAlbum);
    }
    
    [self dismiss];
}

- (void)cameraButtonTouchUpInside:(UIButton *)sender
{
    if (self.completion)
    {
        self.completion(MYSelectCoverTypeCamera);
    }
    
    [self dismiss];
}

- (void)videoButtonTouchUpInside:(UIButton *)sender
{
    if (self.completion)
    {
        self.completion(MYSelectCoverTypeVideo);
    }
    
    [self dismiss];
}

- (void)closeButtonTouchUpInside:(UIButton *)sender
{
    [self dismiss];
}

- (void)dismiss
{
       [UIView animateWithDuration:0.20 animations:^{
           self->my_mainView.transform = CGAffineTransformTranslate(self->my_mainView.transform, 0, self->my_mainView.bounds.size.height);
       } completion:^(BOOL finished) {
           [UIView animateWithDuration:0.20 animations:^{
               self.alpha = 0.0;
           } completion:^(BOOL finished) {
               self.hidden = YES;
               [self removeFromSuperview];
           }];
       }];
}

@end
